
pub struct CurlyBracketMapFlag {}

impl CurlyBracketMapFlag {
    #[inline]
    fn is_high_bit_on_64(len: usize)  -> bool {
        (len & 0x8000000000000000_usize)  != 0
    }
    #[inline]
    fn set_high_bit_on_64(len: usize) -> usize {
        0x8000000000000000_usize | len
    }

    #[inline]
    fn clear_high_bit_on_64(len: usize) -> usize {
        (!0x8000000000000000_usize) & len
    }


    #[inline]
    pub fn has_curly_bracket(len: Option<usize>) -> bool {
        !Self::no_curly_bracket(len)
    }
    #[inline]
    pub fn no_curly_bracket(len: Option<usize>) -> bool {
        if let Some(n) = len {
            Self::is_high_bit_on_64(n)
        } else {
            false
        }
    }

    #[inline]
    pub fn remove_curly_bracket(len: usize) -> Option<usize> {
        Some(Self::set_high_bit_on_64(len))
    }
    /*
    #[inline]
    pub fn add_curly_bracket(len: usize) -> Option<usize> {
        Some(Self::clear_high_bit_on_64(len))
    }
    */
    #[inline]
    pub fn real_len(len: Option<usize>) -> usize {
        if let Some(n) = len {
            Self::clear_high_bit_on_64(n)
        } else {
            0_usize
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;
    #[test]
    fn usize_bit_ops(){
        
        let us = 0x1_usize << 63;
        assert_eq!(0x8000000000000000, us);

        assert_eq!(1_usize, 0x8000000000000000_usize >> 63);


        assert!(CurlyBracketMapFlag::is_high_bit_on_64(0x8000000000000000));
        assert!(!CurlyBracketMapFlag::is_high_bit_on_64(0x7000000000000000));

        assert_eq!(0x8000000000000020_usize, CurlyBracketMapFlag::set_high_bit_on_64(0x20_usize));
        assert_eq!(0x8000000000000120_usize, CurlyBracketMapFlag::set_high_bit_on_64(0x120_usize));

        assert_eq!(0x0000000000000000, CurlyBracketMapFlag::clear_high_bit_on_64(0x8000000000000000));
        assert_eq!(0x0000000000000001, CurlyBracketMapFlag::clear_high_bit_on_64(0x8000000000000001));
        
        assert!(CurlyBracketMapFlag::no_curly_bracket(Some(0x8000000000000000)));
        assert!(CurlyBracketMapFlag::no_curly_bracket(Some(0x8000000000001000)));

        assert!(CurlyBracketMapFlag::has_curly_bracket(Some(0x7000000000001000)));

        assert_eq!(CurlyBracketMapFlag::remove_curly_bracket(1_usize), Some(0x8000000000000001_usize));

        //assert_eq!(CurlyBracketMapFlag::add_curly_bracket(0x8000000000000001_usize), Some(0x1_usize));
    }
}
